#include<iostream>
#include<unistd.h>
using namespace std;

void childwrite(int wfd)
{ while(1)
{
    static int cnt=0;
    cnt++;
 string ptr="i am you child";
 int pid=getpid();
 ptr+=to_string(pid);
 ptr+=" write ";
 ptr+=to_string(cnt);
 ptr+="次";
 write(wfd,ptr.c_str(),ptr.size());
 sleep(1);
}
}
void fatherread(int rfd)
{
 
  while(1)
  {
     char buffer[1024];
   int n=read(rfd,buffer,sizeof(buffer));
   if(n>0)
   {
     buffer[n]=0;
     cout<<buffer<<endl;
   }

  }

}

int main()
{  int fd[2];
  int n=pipe(fd);
  if(n!=0)
  {cout<<"pipe error...."<<endl;}
   cout<<"read:fd[0]"<<fd[0]<<" write:fd[1]"<<fd[1]<<endl;
   int pid=fork();
   if(pid==0)
   {

  
    close(fd[0]);
    childwrite(fd[1]);
    close(fd[1]);
   }


    close(fd[1]);
    fatherread(fd[0]);
    close(fd[0]);

}